package com.websuites.esb.cluster;

import java.io.Serializable;

import org.jgroups.Address;
import org.jgroups.Message;
import org.jgroups.util.Streamable;
import org.jgroups.util.Util;

import com.websuites.core.context.SpringContextHelper;

public class ProxySenderAdapter
{	
	private BroadcastingListener sender;
	
	public void setBroadcastingListener(BroadcastingListener sender)
	{
		this.sender = sender;
	}
	
	public static void sendNotification(Streamable data)
	{
		ProxySenderAdapter senderAdapter = (ProxySenderAdapter)SpringContextHelper.getBean("senderAdapter");
		senderAdapter.send(data);
	}

	public static void sendNotification(Address dest, Streamable data)
	{
		ProxySenderAdapter senderAdapter = (ProxySenderAdapter)SpringContextHelper.getBean("senderAdapter");
		senderAdapter.send(dest, data);
	}

	public static void sendNotification(Address dest, Serializable data)
	{
		ProxySenderAdapter senderAdapter = (ProxySenderAdapter)SpringContextHelper.getBean("senderAdapter");
		senderAdapter.send(dest, data);
	}

	public static void sendNotification(Serializable data)
	{
		ProxySenderAdapter senderAdapter = (ProxySenderAdapter)SpringContextHelper.getBean("senderAdapter");
		senderAdapter.send(data);
	}

	public void send(Streamable data)
	{
		try 
		{
			byte[] buf = Util.objectToByteBuffer(data);
		    Message msg = new Message(null, null, buf);
		    this.sender.send(msg);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public void send(Address dest, Streamable data)
	{
		try 
		{
			byte[] buf = Util.objectToByteBuffer(data);
		    Message msg = new Message(dest, null, buf);
		    this.sender.send(msg);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	
	public void send(Serializable data)
	{
		try 
		{
			byte[] buf = Util.objectToByteBuffer(data);
		    Message msg = new Message(null, null, buf);
		    this.sender.send(msg);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	public void send(Address dest, Serializable data)
	{
		try 
		{
			byte[] buf = Util.objectToByteBuffer(data);
		    Message msg = new Message(dest, null, buf);
		    this.sender.send(msg);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}
